package com.tony.chartdemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.tony.chartdemo.BaseFragment;
import com.tony.chartdemo.R;

import java.util.ArrayList;
import java.util.List;

import static com.github.mikephil.charting.utils.ColorTemplate.rgb;

/**
 * Created by Tony on 2016/7/26.
 */

public class PieChartFragment extends BaseFragment {
    private PieChart pieChart;
    @Override
    public View returnView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_piechart,container,false);
        return view;
    }

    @Override
    public void initView(View view) {
        pieChart= (PieChart) view.findViewById(R.id.pieChart);

        pieChart.setNoDataTextDescription("没有数据");
        pieChart.setDescription("扇形");
        pieChart.setDescriptionColor(getResources().getColor(android.R.color.holo_blue_light));

        Legend l = pieChart.getLegend();
        l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);

    }

    @Override
    protected void initData() {

        List<PieEntry> pieEntryArrayList = new ArrayList<>();
        PieEntry p1 = new PieEntry(35,"春");
        PieEntry p2 = new PieEntry(15,"夏");
        PieEntry p3 = new PieEntry(5,"秋");
        PieEntry p4 = new PieEntry(45,"冬");
        pieEntryArrayList.add(p1);
        pieEntryArrayList.add(p2);
        pieEntryArrayList.add(p3);
        pieEntryArrayList.add(p4);
        PieDataSet dataSet =new PieDataSet(pieEntryArrayList,"扇形图");
        int[] MATERIAL_COLORS = {
                rgb("#2ecc71"), rgb("#f1c40f"), rgb("#e74c3c"), rgb("#3498db")
        };
        dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
        //每个扇区之间的间隔
        dataSet.setSliceSpace(10f);
        //每个扇区被选择时，延时的长度
        dataSet.setSelectionShift(10f);
        PieData pieData = new PieData(dataSet);
        pieData.setValueTextSize(22);

        pieChart.setCenterText("四季");
        pieChart.setCenterTextRadiusPercent(20f);
        pieChart.setCenterTextSize(20f);
        pieChart.setData(pieData);
        pieChart.invalidate();
    }
}
